@using CodeGenerator
@using RazorLight
@using BXJG.Common;
@using BXJG.Common.Extensions;
@inherits TemplatePage<ExecuteContext>
@{
    DisableEncoding = true;//禁止html编码，因为我们是做代码生成器
}

using @(Model.Model.CoreNamespace);
using @Model.Model.CoreShareNamespace;
using @(Model.Model.ApplicationCommonShareNamespace);
using Abp.Authorization;
using BXJG.Utils.Application.Share.GeneralTree;
using BXJG.Utils.Application.Share.Dtos;
namespace @(Model.Model.ApplicationCommonNamespace)
{
    /// <summary>
    /// 获取 @(Model.Model.DisplayName) 以供选择
    ///</summary>
    [AbpAuthorize]
    public partial class @(Model.Model.ProviderAppService) :
@if(Model.Model.IsTree){<text>
                                                     CommonTreeProviderBaseAppService<@(Model.Model.EntityName), 
                                                                                      @(Model.Model.ProviderCondition),
                                                                                      @(Model.Model.ProviderDtoName),
                                                                                      GeneralTreeGetForSelectInput,
                                                                                      GeneralTreeComboboxDto>
</text>}
else
{
                                                    @:CommonProviderBaseAppService<@(Model.Model.EntityName),PagedAndSortedResultRequest<@(Model.Model.ProviderCondition)>,@(Model.Model.ProviderDtoName),@(Model.Model.PrimaryField.CSharpType)>
}
    {
@if(Model.Model.IsTree)
{
        @:protected override IQueryable<@(Model.Model.EntityName)> ComboTreeFilter(@(Model.Model.ProviderCondition) condition, string parentCode)
}else{
        @:protected override IQueryable<@(Model.Model.EntityName)> CreateFilteredQuery(PagedAndSortedResultRequest<@(Model.Model.ProviderCondition)> input)
}       
        {
    @if (Model.Model.IsTree)
    {
        <text>
            var query = base.ComboTreeFilter(condition, parentCode);
        </text>
    }
    else
    {  <text>
            var query = base.CreateFilteredQuery(input);
            var condition = input.Filter;
    </text>
}

@if(Model.Model.ConditionStringFields.Any())
{
    var idx = 0;
            @:query = query.WhereIf(condition.Keywords.IsNotNullOrWhiteSpaceBXJG(), x => 
@foreach (var field in Model.Model.ConditionStringFields)
{
            if (idx>0)
            {
                @:|| x.@(field.Name).Contains(condition.Keywords)
            }else{
                @:x.@(field.Name).Contains(condition.Keywords)
            }
    idx++; 
}
            @:);
}

@foreach (var field in Model.Model.ConditionRangeFields)
{
            @:query = query.WhereIf(condition.@(field.ConditionRangeMin).HasValue,x=> x.@(field.Name) >= condition.@(field.ConditionRangeMin).Value );
            @:query = query.WhereIf(condition.@(field.ConditionRangeMax).HasValue,x=> x.@(field.Name) < condition.@(field.ConditionRangeMax).Value );
}

@foreach (var field in Model.Model.ConditionNormalFields)
{
            @:query = query.WhereIf(condition.@(field.Name).HasValue,x=> x.@(field.Name) == condition.@(field.Name).Value );
}

            return query;
        }
    }
}